﻿using System;
using System.Collections.Generic;
using System.Reflection;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEditor;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{
    
    [Serializable]
    public class AnimationAdaptConfig
    {
      
        public bool enabled = true;
        
        public AnimationCullingType cullingType;
        public bool playAutomatically;
        public List<AnimationClip> clip = new List<AnimationClip>(0);
        public AnimationClip defaultClip;
        //重写Equals方法
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            AnimationAdaptConfig other = (AnimationAdaptConfig)obj;
            // Compare enabled properties
            if (enabled != other.enabled)
            {
                return false;
            }
            
            // Compare cullingType properties
            if (cullingType != other.cullingType)
            {
                return false;
            }
            
            // Compare playAutomatically properties
            if (playAutomatically != other.playAutomatically)
            {
                return false;
            }
            
            // Compare clip properties
            if (clip.Count != other.clip.Count)
            {
                return false;
            }
            
            for (int i = 0; i < clip.Count; i++)
            {
                if (clip[i] != other.clip[i])
                {
                    return false;
                }
            }
            
            // Compare defaultClip properties
            if (defaultClip != other.defaultClip)
            {
                return false;
            }
            
            return true;
        }
        
        // When you override Equals, you should also override GetHashCode
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + (enabled ? 1 : 0);
            hash = hash * 23 + (cullingType != null ? cullingType.GetHashCode() : 0);
            hash = hash * 23 + (playAutomatically ? 1 : 0);
            hash = hash * 23 + (clip != null ? clip.GetHashCode() : 0);
            hash = hash * 23 + (defaultClip != null ? defaultClip.GetHashCode() : 0);
            return hash;
        }
        
    }

    
    [DisallowMultipleComponent]
    public class AnimationAdapt: AdaptBase<AnimationAdaptConfig, Animation>
    {
        protected override void ApplyConfig(AnimationAdaptConfig config)
        {
            base.ApplyConfig(config);
            if (mComponent.isPlaying)
            {
                mComponent.Stop();
            }

#if UNITY_EDITOR
            AnimationClip[] clips = AnimationUtility.GetAnimationClips(mComponent.gameObject);
            foreach (var clip in clips)
            {
                mComponent.RemoveClip(clip);
            }
#else
            var clipCount = mComponent.GetClipCount();
            List<string> clipNameList = new List<string>(clipCount);
            foreach (AnimationState state in mComponent)
            {
                clipNameList.Add(state.name);
            }
            foreach (var clipName in clipNameList)
            {
                var clip = mComponent.GetClip(clipName);
                if (clip != null)
                {
                    mComponent.RemoveClip(clip);
                }
            }
#endif
            for (int i = 0; i < config.clip.Count; i++)
            {
                mComponent.AddClip(config.clip[i], config.clip[i].name);
            }
            
            mComponent.enabled = config.enabled;
            mComponent.clip = config.defaultClip;
            mComponent.playAutomatically = config.playAutomatically;
            mComponent.cullingType = config.cullingType;
            
            if (mComponent.clip != null && mComponent.gameObject.activeInHierarchy &&
                mComponent.enabled && mComponent.playAutomatically)
            {
                mComponent.Play(mComponent.clip.name);
            }
        }
        
        protected override void CopyProperty(AnimationAdaptConfig config)  
        {
            base.CopyProperty(config);
#if UNITY_EDITOR
            AnimationClip[] clips = AnimationUtility.GetAnimationClips(mComponent.gameObject);
            config.clip = new List<AnimationClip>(clips);
            
            config.enabled = mComponent.enabled;
            config.defaultClip = mComponent.clip;
            config.playAutomatically = mComponent.playAutomatically;
            config.cullingType = mComponent.cullingType;
#endif

        }
    }

  
}